<?php
header ("Content-Type:text/html; charset= utf-8");
require_once ('config.php');
/*用于生成验证码*/
$font_size  =14;
$img_height =24;
$img_width  =78;
$font_file  =WEBURL."/data/validate_code_image/arial.ttf";
$word_file=WEBURL."/data/validate_code_image/words.txt";

if (!echo_validate_image($font_size,$img_height ,
$img_width,$font_file ,$word_file))
{
	// 如果不成功则初始化一个默认验证码
	$_SESSION['valiate_image_value'] = 'DHBC';
	$im = @imagecreatefrompng('WEBURL."/data/validate_code_image/default.jpg');
	header("Pragma:no-cache\r\n");
	header("Cache-Control:no-cache\r\n");
	header("Expires:0\r\n");
	imagejpeg($im);
	imagedestroy($im);
}

function echo_validate_image($font_size, $img_height,$img_width,$font_file,$word_file)
{
	//创建图片，并设置背景色
	$im = @imagecreate($img_width, $img_height);
	imagecolorallocate($im, 255,255,255);

	//文字随机颜色
	$fontColor[]  = imagecolorallocate($im, 0x15, 0x15, 0x15);
	$fontColor[]  = imagecolorallocate($im, 0x95, 0x1e, 0x04);
	$fontColor[]  = imagecolorallocate($im, 0x93, 0x14, 0xa9);
	$fontColor[]  = imagecolorallocate($im, 0x12, 0x81, 0x0a);
	$fontColor[]  = imagecolorallocate($im, 0x06, 0x3a, 0xd5);

	//获取随机字符
	$rndstring  = '';
		for($i=0; $i<4; $i++)
		{
			$tmp=mt_rand(49, 90);
				if($tmp>57&&$tmp<65){
					$tmp+=8;
				}
				$c = chr($tmp);
				if($c=="I"||$c=="O"){
					$c="A";
				}
				
				//echo $c;
			$rndstring .= $c;
		}
		//echo $rndstring."\n";
	if($rndstring==""||strlen($rndstring)!=4)
	{
		$fp = @fopen($word_file, 'rb');
		if (!$fp) return FALSE;

		$fsize = filesize($word_file);
		if ($fsize < 32) return FALSE;
		if ($fsize < 128)
		{
			$max = $fsize;
		} else {
			$max = 128;
		}
		fseek($fp, rand(0, $fsize - $max), SEEK_SET);
		$data = fread($fp, 128);
		fclose($fp);
		$data = preg_replace("/\r?\n/", "\n", $data);
		$start = strpos($data, "\n", rand(0, 100)) + 1;
		$end = strpos($data, "\n", $start);
		$rndstring  = substr($data, $start, $end - $start);
		$rndstring=strtoupper($rndstring);
	}
    $rndcodelen = strlen($rndstring);
   // echo $rndcodelen;
  // echo $rndstring;
    $_SESSION['valiate_image_value'] = $rndstring;
	//背景横线
	 $lineColor1 = imagecolorallocate($im, 0xda, 0xd9, 0xd1);
   for($j=0; $j<=$img_height-3; $j=$j+3)
	{
		imageline($im, 2, $j, $img_width - 2, $j, $lineColor1);
	}

	//背景竖线
	$lineColor2 = imagecolorallocate($im, 0xda,0xd9,0xd1);
   for($j=2;$j<100;$j=$j+6)
	{
		imageline($im, $j, 0, $j+6, $img_height, $lineColor2);
	}

	//画边框
	
		$bordercolor = imagecolorallocate($im, 0x9d, 0x9e, 0x96);
		imagerectangle($im, 0, 0, $img_width-1, $img_height-1, $bordercolor);

	//输出文字 
	
	for($i=0;$i<$rndcodelen;$i++)
	{
		$bc = mt_rand(0, 1);
		$rndstring[$i] = strtoupper($rndstring[$i]);
		$c_fontColor = $fontColor[mt_rand(0,4)];
		$x_pos = $i==0 ? 8 : $i*($font_size+7);
		$c = mt_rand(0, 15);
		@imagettftext($im, $font_size, $c,$x_pos,20, $c_fontColor, $font_file, $rndstring[$i]);
	}
	//图象效果	
	imagefilter ( $im, IMG_FILTER_GRAYSCALE);
	//输出前准备工作
    header("Pragma:no-cache\r\n");
	header("Cache-Control:no-cache\r\n");
	header("Expires:0\r\n");
	//输出特定类型的图片格式
	if(function_exists("imagejpeg"))
	{
		header("content-type:image/jpeg\r\n");
		imagejpeg($im);
	}
	else
	{
		header("content-type:image/png\r\n");
		imagepng($im);
	}
	imagedestroy($im);
	exit();
	
}